<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true"><block slot="backText">返回</block><block slot="content">请选择</block></cu-custom>
    <ly-tree :tree-data="treeData" 
        node-key="label" 
        @node-expand="handleNodeExpand" 
        @node-click="handleNodeClick"
		>
        </ly-tree>
		<button @click="showAll" class="cu-btn bg-green margin-tb-sm lg" style="width:100%">确定</button>
		</view>
</template>
<script>
	import LyTree from './tree/ly-tree.vue';
	import {mapState, mapActions} from 'vuex';
	export default {
	    components: {
	        LyTree
	    },
	    data() {
	        return {
	            treeData: [],
				myData:[]
	        };
	    },
	    onLoad() {
	        setTimeout(() => {
				this.getDictValue();
	            this.treeData = [{
	                label: '一级 1',
	                children: [{
	                    label: '二级 1-1',
	                    children: [{
	                        label: '三级 1-1-1'
	                    }]
	                }]
	            }, {
	                label: '一级 2',
	                children: [{
	                    label: '二级 2-1',
	                    children: [{
	                        label: '三级 2-1-1'
	                    }]
	                }, {
	                    label: '二级 2-2',
	                    children: [{
	                        label: '三级 2-2-1'
	                    }]
	                }]
	            }, {
	                label: '一级 3',
	                children: [{
	                    label: '二级 3-1',
	                    children: [{
	                        label: '三级 3-1-1'
	                    }]
	                }, {
	                    label: '二级 3-2',
	                    children: [{
	                        label: '三级 3-2-1'
	                    }]
	                }]
	            }, {
	                label: '一级 3',
	                children: [{
	                    label: '二级 3-1',
	                    children: [{
	                        label: '三级 3-1-1'
	                    }]
	                }, {
	                    label: '二级 3-2',
	                    children: [{
	                        label: '三级 3-2-1'
	                    }]
	                }]
	            }, {
	                label: '一级 3',
	                children: [{
	                    label: '二级 3-1',
	                    children: [{
	                        label: '三级 3-1-1'
	                    }]
	                }, {
	                    label: '二级 3-2',
	                    children: [{
	                        label: '三级 3-2-1'
	                    }]
	                }]
	            }]
	        }, 2000);
	    },
	    methods: {
			...mapActions(["JAjax"]),
	        // uni-app中emit触发的方法只能接受一个参数
	        handleNodeClick(obj) {
	            console.log('handleNodeClick', JSON.stringify(obj));
	        },
	        handleNodeExpand(obj) {
	            console.log('handleNodeExpand', JSON.stringify(obj));
	        },
			showAll(){
				console.log(JSON.stringify(this.myData))
			},
			getAllSub(allData, myData){
				myData.forEach((value, key)=>{
					allData.forEach((v, k)=>{
						if(value.id == v.pId){
							value.children == undefined?value.children = []:"";
							var obj = {}
							obj.label = v.name;
							obj.id = v.id;
							obj.pId = v.pId;
							console.log(obj.label);
							value.children.push(obj);
						}
					})
					if(value.children && value.children.length > 0){
						this.getAllSub(allData, value.children);
					}
				})
				console.info(this.myData)
			},
			getDictValue(){
				this.JAjax({
					url: '/a/sys/office/treeData?isLoadUser=true',
					success: data=>{
						let result = data.data;
						result.forEach((value, key)=>{
							if(value.pId == "0"){
								var obj = {}
								obj.label = value.name;
								obj.id = value.id;
								obj.pId = value.pId;
								this.myData.push(obj)
							}
						})
						this.getAllSub(result, this.myData);
					}
				});
				// uni.request({
				// 	url: this.HOST_URL + '/a/sys/dictData/listData?dictType=' + this.dict + '&t=' + new Date().getTime() + '&__sid=' +
				// 		this.sessionId,
				// 	success: (data) => {
				// 		if (data.statusCode == 200 && data.data) {
				// 			this.picker = []
				// 			for (var i = 0; i < data.data.length; i++) {
				// 				this.picker.push(data.data[i].dictLabel)
				// 				this.pickerValue.push(data.data[i].dictValue)
				// 			}
				// 		}
				// 	},
				// 	fail: (data, code) => {
						
				// 	},
				// })
			}
	    }
	};
</script>
<style>
	@import url("../../colorui/ly-tree.css");
</style>